package com.sibrary.booxoo.download;

import java.util.ArrayList;
import java.util.List;

public class BooxooDownloadEventController {

	private List<BooxooIDownloadListener> mDownloadListeners;

	/**
	 * Holder for singleton implementation.
	 */
	private static class BooxooDownloadEventControllerHolder {
		private static final BooxooDownloadEventController INSTANCE = new BooxooDownloadEventController();
	}

	/**
	 * Get the unique instance of the Controller.
	 * 
	 * @return The instance of the Controller
	 */
	public static BooxooDownloadEventController getInstance() {
		return BooxooDownloadEventControllerHolder.INSTANCE;
	}

	/**
	 * Private Constructor.
	 */
	private BooxooDownloadEventController() {
		mDownloadListeners = new ArrayList<BooxooIDownloadListener>();
	}

	/**
	 * 添加监视器
	 * 
	 * @param listener
	 */
	public synchronized void addListener(BooxooIDownloadListener listener) {
		if (!mDownloadListeners.contains(listener)) {
			mDownloadListeners.add(listener);
		}
	}

	/**
	 * 删除监视器
	 * 
	 * @param listener
	 */
	public synchronized void removeListener(BooxooIDownloadListener listener) {
		mDownloadListeners.remove(listener);
	}

	/**
	 * 执行回调
	 * 
	 * @param msg
	 */
	public synchronized void trigger(int type, Object obj) {
		for (int i = 0; i < mDownloadListeners.size(); i++) {
			mDownloadListeners.get(i).onDownloadEvent(type, obj);
		}
	}
}
